﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlPutawayProcess
    {
        /// <summary>
        /// 2.0原料上架库位库存 Add By Quinn.Ling 201705
        /// </summary>
        public static string GetPutaway(string xml)
        {
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;
            App.Connection();
            App.DBconSet();

            //1接收上架XML
            DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
            if (ldt.Rows.Count == 0)
            {
                return "";
            }

            //2拆分组合新的ASN上架表--获取库位
            DataTable ldt_sj = LicPdaService.App.GetNewDataTabel(ldt);
            //3获取库位
            //3.1补充信息customer_code,test
            strSql = "select customer_code from Receipt where receiptkey='" + ldt_sj.Rows[0]["asn"].ToString() + "'";
            DataTable ldt_customer_code = App.GetDataTable(App.DBconWMS, strSql, "Sku");

            for (int i = 0; i < ldt_sj.Rows.Count; i++)
            {
                if (ldt_customer_code.Rows.Count > 0)
                {
                    ldt_sj.Rows[i]["customer_code"] = ldt_customer_code.Rows[0]["customer_code"].ToString();
                }

                strSql = "select sku from Sku where customer_code='"+ ldt_sj.Rows[i]["customer_code"] + "' and  sku='" + ldt_sj.Rows[i]["sku"].ToString() + "'";
                DataTable ldt_test = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                if (ldt_test.Rows.Count > 0)
                {
                    ldt_sj.Rows[i]["test"] = ldt_test.Rows[0]["sku"].ToString();
                }
            }
            string lskw = "UNKNOW";
            //lskw = App.GetPutAway(ldt_sj);
            if (lskw != "")
            {
                for (int i = 0; i < ldt_sj.Rows.Count; i++)
                {
                    ldt_sj.Rows[i]["storage"] = lskw;
                }

            }
            return GetPutaway_Return(ldt_sj);
        }

        //上架--分配库位返回值
        private static string GetPutaway_Return(DataTable ldt)
        {
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");

            for (int i = 0; i < ldt.Rows.Count; i++)
            {
                XmlNode putawayNodes = doc.CreateElement("putaway_return");//返回用户节点
                XmlElement lxeuserid = doc.CreateElement("userid");
                XmlElement lxeasn = doc.CreateElement("asn");
                XmlElement lxewhseid = doc.CreateElement("whseid");
                XmlElement lxemaster_label = doc.CreateElement("master_label");
                XmlElement lxesku = doc.CreateElement("sku");
                XmlElement lxeqty = doc.CreateElement("qty");
                XmlElement lxestorage = doc.CreateElement("storage");
                XmlElement lxereturn_status = doc.CreateElement("return_status");

                lxeuserid.InnerText = ldt.Rows[i]["userid"].ToString();
                lxeasn.InnerText = ldt.Rows[i]["asn"].ToString();
                lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                lxemaster_label.InnerText = ldt.Rows[i]["master_label"].ToString();
                lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                lxestorage.InnerText = ldt.Rows[i]["storage"].ToString();
                lxereturn_status.InnerText = ldt.Rows[i]["msg"].ToString();
                putawayNodes.AppendChild(lxeuserid);
                putawayNodes.AppendChild(lxewhseid);
                putawayNodes.AppendChild(lxeasn);
                putawayNodes.AppendChild(lxemaster_label);
                putawayNodes.AppendChild(lxesku);
                putawayNodes.AppendChild(lxestorage);
                putawayNodes.AppendChild(lxereturn_status);
                rootRet.AppendChild(putawayNodes);
            }

            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }
    }
}
